// $Id$
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Xml.Serialization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;
#if (!NET_CF)
using System.Diagnostics.CodeAnalysis;
#endif

namespace Northwind.DomainModel
{

    /// <summary />
    partial interface IOrderContainer
    {
        /// <summary />
      Order Order
        {
            get;
            set;
        }

    }

    /// <summary />
    partial interface IOrderMapper
    {

        /// <summary />
        Order Find(Order.PrimaryKey key, DataMapperCollection dataMappers);
        /// <summary />
        DomainModelKeyedCollection<Order> FindAll(DataMapperCollection dataMappers);

    }

    /// <summary />
    partial class Order
    {

        /// <summary />
        partial class PrimaryKey
        {
            /// <summary />
            public static implicit operator PrimaryKey(int key)
            {
                return new PrimaryKey(key);
            }

            /// <summary />
            public static explicit operator int(PrimaryKey key)
            {
                return key.Value;
            }

        }

        /// <summary />
        partial class DataTransfer
        {
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int OrderID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string CustomerID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int? EmployeeID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int? ShipVia;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public System.DateTime? OrderDate;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public System.DateTime? RequiredDate;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public System.DateTime? ShippedDate;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public decimal? Freight;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipName;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipAddress;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipCity;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipRegion;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipPostalCode;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public string ShipCountry;

            /// <summary />
            public object Clone()
            {
                return MemberwiseClone();
            }

        }

        /// <summary />
        protected override Order.PrimaryKey CreateId()
        {
            return new Order.PrimaryKey(Data.OrderID);
        }

        /// <summary />
        internal Order.PrimaryKey GetId()
        {
            return Id;
        }

        /// <summary />
        internal void SetId(int id)
        {
            Data.OrderID=id;
        }


        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, false)]
#endif
        public static Order Find(Order.PrimaryKey key, DataMapperCollection dataMappers)
        {
            return ((IOrderMapper)dataMappers[typeof(Order)]).Find(key, dataMappers);
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, true)]
#endif
        public static IList<Order> FindAll(DataMapperCollection dataMappers)
        {
            return ((IOrderMapper)dataMappers[typeof(Order)]).FindAll(dataMappers);
        }

        /// <summary />
        private void _LoadCustomer(bool lazyload)
        {
            if (IsNew)
                return;

            if (_Customer==null)
                _Customer=((ICustomerMapper)DataMappers[typeof(Customer)]).Find(Data.CustomerID, DataMappers);
        }

        /// <summary />
        private void _Customer_Saved(object sender, PersistenceEventArgs e)
        {
            string id=((Customer)sender).GetId().Value;
            if (Data.CustomerID!=id)
            {
                Data.CustomerID=id;
                IsModified=true;
            }

            ((Customer)sender).Saved-=_Customer_Saved;
        }

        /// <summary />
        private void _LoadEmployee(bool lazyload)
        {
            if (IsNew)
                return;

            if ((_Employee==null) && (Data.EmployeeID.HasValue))
                _Employee=((IEmployeeMapper)DataMappers[typeof(Employee)]).Find(Data.EmployeeID, DataMappers);
        }

        /// <summary />
        private void _Employee_Saved(object sender, PersistenceEventArgs e)
        {
            int? id=((Employee)sender).GetId().Value;
            if (Data.EmployeeID!=id)
            {
                Data.EmployeeID=id;
                IsModified=true;
            }

            ((Employee)sender).Saved-=_Employee_Saved;
        }

        /// <summary />
        private void _LoadShipper(bool lazyload)
        {
            if (IsNew)
                return;

            if ((_Shipper==null) && (Data.ShipVia.HasValue))
                _Shipper=((IShipperMapper)DataMappers[typeof(Shipper)]).Find(Data.ShipVia, DataMappers);
        }

        /// <summary />
        private void _Shipper_Saved(object sender, PersistenceEventArgs e)
        {
            int? id=((Shipper)sender).GetId().Value;
            if (Data.ShipVia!=id)
            {
                Data.ShipVia=id;
                IsModified=true;
            }

            ((Shipper)sender).Saved-=_Shipper_Saved;
        }

        /// <summary />
        internal bool InternalIsNew
        {
            get
            {
                return IsNew;
            }
        }

        /// <summary />
        private Customer _CustomerProperty
        {
            get
            {
                Load(true);
                return _Customer;
            }
            set
            {
                _LoadCustomer(true);
                if (_Customer!=value)
                {
                    if ((_Customer!=null) && (_Customer.InternalIsNew))
                        _Customer.Saved-=_Customer_Saved;
                    _Customer=value;
                    if (value!=null)
                    {
                        if (_Customer.InternalIsNew)
                            _Customer.Saved+=_Customer_Saved;
                        else
                            Data.CustomerID=(string)value.GetId();
                    }
                }
            }
        }

        /// <summary />
        private Employee _EmployeeProperty
        {
            get
            {
                Load(true);
                return _Employee;
            }
            set
            {
                _LoadEmployee(true);
                if (_Employee!=value)
                {
                    if ((_Employee!=null) && (_Employee.InternalIsNew))
                        _Employee.Saved-=_Employee_Saved;
                    _Employee=value;
                    if (value!=null)
                    {
                        if (_Employee.InternalIsNew)
                            _Employee.Saved+=_Employee_Saved;
                        else
                            Data.EmployeeID=(int?)value.GetId();
                    } else
                      Data.EmployeeID=null;
                }
            }
        }

        /// <summary />
        private Shipper _ShipperProperty
        {
            get
            {
                Load(true);
                return _Shipper;
            }
            set
            {
                _LoadShipper(true);
                if (_Shipper!=value)
                {
                    if ((_Shipper!=null) && (_Shipper.InternalIsNew))
                        _Shipper.Saved-=_Shipper_Saved;
                    _Shipper=value;
                    if (value!=null)
                    {
                        if (_Shipper.InternalIsNew)
                            _Shipper.Saved+=_Shipper_Saved;
                        else
                            Data.ShipVia=(int?)value.GetId();
                    } else
                      Data.ShipVia=null;
                }
            }
        }

        /// <summary />
        private System.DateTime? _OrderDateProperty
        {
            get
            {
                Load(true);
                return Data.OrderDate;
            }
            set
            {
                if (Data.OrderDate!=value)
                {
                    Data.OrderDate=value;
                }
            }
        }

        /// <summary />
        private System.DateTime? _RequiredDateProperty
        {
            get
            {
                Load(true);
                return Data.RequiredDate;
            }
            set
            {
                if (Data.RequiredDate!=value)
                {
                    Data.RequiredDate=value;
                }
            }
        }

        /// <summary />
        private System.DateTime? _ShippedDateProperty
        {
            get
            {
                Load(true);
                return Data.ShippedDate;
            }
            set
            {
                if (Data.ShippedDate!=value)
                {
                    Data.ShippedDate=value;
                }
            }
        }

        /// <summary />
        private decimal? _FreightProperty
        {
            get
            {
                Load(true);
                return Data.Freight;
            }
            set
            {
                if (Data.Freight!=value)
                {
                    Data.Freight=value;
                }
            }
        }

        /// <summary />
        private string _ShipNameProperty
        {
            get
            {
                Load(true);
                return Data.ShipName;
            }
            set
            {
                if (Data.ShipName!=value)
                {
                    Data.ShipName=value;
                }
            }
        }

        /// <summary />
        private string _ShipAddressProperty
        {
            get
            {
                Load(true);
                return Data.ShipAddress;
            }
            set
            {
                if (Data.ShipAddress!=value)
                {
                    Data.ShipAddress=value;
                }
            }
        }

        /// <summary />
        private string _ShipCityProperty
        {
            get
            {
                Load(true);
                return Data.ShipCity;
            }
            set
            {
                if (Data.ShipCity!=value)
                {
                    Data.ShipCity=value;
                }
            }
        }

        /// <summary />
        private string _ShipRegionProperty
        {
            get
            {
                Load(true);
                return Data.ShipRegion;
            }
            set
            {
                if (Data.ShipRegion!=value)
                {
                    Data.ShipRegion=value;
                }
            }
        }

        /// <summary />
        private string _ShipPostalCodeProperty
        {
            get
            {
                Load(true);
                return Data.ShipPostalCode;
            }
            set
            {
                if (Data.ShipPostalCode!=value)
                {
                    Data.ShipPostalCode=value;
                }
            }
        }

        /// <summary />
        private string _ShipCountryProperty
        {
            get
            {
                Load(true);
                return Data.ShipCountry;
            }
            set
            {
                if (Data.ShipCountry!=value)
                {
                    Data.ShipCountry=value;
                }
            }
        }

        /// <summary />
        private Customer _Customer;
        /// <summary />
        private Employee _Employee;
        /// <summary />
        private Shipper _Shipper;

        /// <summary />
        public static readonly IdentityMap<Order.PrimaryKey, Order> IdentityMap=new IdentityMap<Order.PrimaryKey, Order>();

    }

}
